abstract class State {
  stateName: string
  abstract Handle(context: Context): void
}

class State_A extends State {
  constructor() {
    super()
    this.stateName = '状态A'
  }

  Handle(context: Context): void {
    context.setState(new State_B())
  }
}

class State_B extends State {
  constructor() {
    super()
    this.stateName = '状态B'
  }

  Handle(context: Context): void {
    context.setState(new State_A())
  }
}

class Context {
  private state: State
  constructor(state: State) {
    this.state = state
  }

  setState(state: State) {
    console.log(`当前状态为：${this.state.stateName}`)
    this.state = state
    console.log(`更改状态为：${this.state.stateName}`)
    console.log('------------------------')
  }

  Request() {
    this.state.Handle(this)
  }
}

(function () {
  let context = new Context(new State_A())

  context.Request()
  context.Request()
  context.Request()
}())